package com.example.homeworkmutualevaluation.Dao;

import com.example.homeworkmutualevaluation.Entity.Message;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Repository
public interface MessageDao extends JpaRepository<Message, String> {
    @Transactional
    @Modifying
    @Query(value = "INSERT INTO message(`id`, `tphone`, `sphone`, `title`, `content`) VALUES (?1, ?2, ?3, ?4, ?5)", nativeQuery = true)
    int insert(String id, String tphone, String sphone, String title, String content);

    @Transactional
    @Modifying
    @Query(value = "UPDATE message SET `read`=TRUE WHERE `id`=?1", nativeQuery = true)
    int onRead(String id);

    @Query(value = "SELECT * FROM message WHERE `sphone`=?1 ORDER BY `read` DESC, `datetime` DESC", nativeQuery = true)
    List<Message> getAllMsg(String sphone);
}
